package com.item.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.Cart;
import com.item.mall.entity.User;
import com.item.mall.entity.UserAddress;
import com.item.mall.service.CartService;
import com.item.mall.service.ProductCategoryService;
import com.item.mall.service.ProductService;
import com.item.mall.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    ProductService productService;
    @Autowired
    UserAddressService userAddressService;

    @GetMapping("/save/{productId}/{price}/{quantity}")
    public ModelAndView saveCart(@PathVariable("productId") Integer productId,
                                 @PathVariable("price") Float price,
                                 @PathVariable("quantity") Integer quantity,
                                 HttpSession session) {
        Cart cart = new Cart();
        cart.setUserId(((User) session.getAttribute("user")).getId());
        cart.setCost(quantity * price);
        cart.setQuantity(quantity);
        cart.setProductId(productId);
        cartService.save(cart);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productDetail");
        modelAndView.addObject("list",productCategoryService.getAllProductCategoryVO());
        modelAndView.addObject("product",productService.getById(productId));
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }

    @GetMapping("/getList")
    public ModelAndView getCartList(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }

    @GetMapping("/removeCart/{id}")
    public String deleteById(@PathVariable("id") Integer productId){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("product_id",productId);
        cartService.remove(queryWrapper);
        return "forward:/cart/getList";
    }

    @PostMapping("/update/{productId}/{quantity}/{cost}")
    @ResponseBody
    public String addQuantity(@PathVariable("productId") Integer productId,
                              @PathVariable("quantity") Integer quantity,
                              @PathVariable("cost") Float cost,
                              HttpSession session){
        Integer userId =((User) session.getAttribute("user")).getId();
        if(cartService.addQuantity(productId, quantity, cost, userId)){
            return "1";
        }
        return "0";
    }

    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("addressList",userAddressService.list());
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_id", user.getId());
        List<UserAddress> addressList = userAddressService.list(queryWrapper);
        modelAndView.addObject("addressList",addressList);
        return modelAndView;
    }

    @PostMapping("/save1/{productId}/{price}/{quantity}")
    public void saveCart1(@PathVariable("productId") Integer productId,
                                 @PathVariable("price") Float price,
                                 @PathVariable("quantity") Integer quantity,
                                 HttpSession session) {
        Cart cart = new Cart();
        cart.setUserId(((User) session.getAttribute("user")).getId());
        cart.setCost(quantity * price);
        cart.setQuantity(quantity);
        cart.setProductId(productId);
        cartService.save(cart);
    }
}

